package com.byron.pss.web.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.byron.pss.dao.ILostFoundDAO;
import com.byron.pss.dao.IMessageDAO;
import com.byron.pss.dao.impl.LostFoundDAOImpl;
import com.byron.pss.dao.impl.MessageDAOImpl;
import com.byron.pss.domain.Const;
import com.byron.pss.domain.LostFoundStr;
import com.byron.pss.domain.Message;
import com.byron.pss.util.StringUtils;
import com.byron.pss.util.Transform;

/**
 * 处理公开页面信息
 * @author Byron
 *
 */
@WebServlet("/index")
public class IndexServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	private ILostFoundDAO dao = null;
	private IMessageDAO msgDao = null;

	@Override
	public void init() throws ServletException {
		dao = new LostFoundDAOImpl();
		msgDao = new MessageDAOImpl();
	}

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		// 拿到标识
		String cmd = req.getParameter("cmd");
		if ("msg".equals(cmd)) {
			if (!checkImagecode(req, resp)) {
				req.getRequestDispatcher("/unchecked/message.jsp").forward(req,
						resp);
				return;
			}
			// 从留言板过来的
			String name = req.getParameter("name");// 必填
			String email = req.getParameter("email");
			String phone = req.getParameter("phone");// 必填
			String lostName = req.getParameter("lostName");// 必填
			String other = req.getParameter("other");
			// 只要姓名和电话为空，那就返回
			if (!StringUtils.hasLength(name) || !StringUtils.hasLength(phone)) {
				// 请求转发，传递数据到显示jsp
				req.setAttribute("errorMsg","姓名和电话为空");
				req.getRequestDispatcher("/unchecked/message.jsp").forward(req,
						resp);
				return;
			}
			if(phone.length()>11){
				req.setAttribute("errorMsg","电话长度不得11位");
				req.getRequestDispatcher("/unchecked/message.jsp").forward(req,
						resp);
				return;
			}
			Message msg = new Message(null, name, email, phone, lostName, other);
			// 保存留言信息
			msgDao.save(msg);
			resp.sendRedirect(req.getContextPath() + "/unchecked/msgsuc.html");
			return;
		}

		// 封装要展示的信息
		List<LostFoundStr> list = Transform.list0jsp(dao.list());
		req.setAttribute("list", list);

		// 请求转发，传递数据到显示jsp
		req.getRequestDispatcher("/WEB-INF/views/index.jsp").forward(req, resp);
	}

	/**
	 * 检测验证码是否正确，另外有errorMsg中已经封装了错误信息
	 * @param req 请求信息
	 * @param resp 响应信息
	 * @return true表示成功，false表示错误
	 */
	private boolean checkImagecode(HttpServletRequest req,
			HttpServletResponse resp) {
		// 拿到用户的验证码
		String imagecode = req.getParameter("imagecode");
		// 拿到session中存储的验证码
		String randomcode_in_session = (String) req.getSession().getAttribute(
				Const.RANDOMCODE_IN_SESSION);
		int check = StringUtils.check(imagecode, randomcode_in_session);
		if (check == 1) {
			// 验证不通过，提示验证码输入错误
			req.setAttribute("errorMsg", "验证码输入错误");
			return false;
		}
		if (check == 2) {
			// 返回到页面，并且提示一个错误信息
			req.setAttribute("errorMsg", "验证码不能为空");
			return false;
		}
		return true;
	}

}
